package com.itic.market.mobile.uc.order;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.market.mobile.base.BaseMobileInterface;
import com.itic.market.system.orderproducts.persistence.model.OrderProducts;
import com.itic.market.system.orderproducts.persistence.model.OrderProductsExample;
import com.itic.market.system.orderproducts.persistence.model.OrderProductsExample.Criteria;
import com.itic.market.system.orderproducts.service.OrderProductsService;
import com.itic.market.system.product.persistence.model.Product;
import com.itic.market.system.product.service.ProductService;
import com.itic.market.system.user.persistence.model.MarketUser;


/**
 * 用户绑定手机号码接口类
 * @author 许小宝
 * 2017年07月26日  14时11分
 */
@Controller
@RequestMapping(value="/mobile/uc/order")
public class MarketUcOrderInterface
     extends BaseMobileInterface {
	
	@Autowired
	private ProductService productService;
	@Autowired
	private OrderProductsService orderProductsService;
	
	/**
     * 用户手机号码发送验证码
     * @return 
     */
    @RequestMapping(value="/list")
    @ResponseBody
    public Map<String,Object> send( HttpServletRequest request, HttpServletResponse response, String token, String orderType ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		//判断必要的参数
    		if( StringHelper.isNotBlank(token) && StringHelper.isNotBlank(orderType) ){
    			Map<String, Object> dataMap = new HashMap<String,Object>();
    			MarketUser user = getMarketUserByToken(token);
    			if( null != user ){
    				OrderProductsExample example = new OrderProductsExample();
    				example = this.handlePageQuery(request, example);
    				example.setOrderByClause("CREATE_TIME DESC");
    				
    				Criteria criteria = example.createCriteria();
    				criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    				criteria.andMarketUserIdEqualTo(user.getId());
    				
    				//criteria.andIsPayEqualTo(DBConst.TRUE);
    				//过滤付款后未确认收货的情况
    				
    			    if("0".equals(orderType)){
    			    	//criteria.andStatusEqualTo('1');
    			    	criteria.andStatusEqualTo("1");
    			    	//criteria.andIsComebackEqualTo(DBConst.FALSE);
    			    }
    			    //过滤付款后已确认收货的情况，未退换货的情况
    			    if("1".equals(orderType)){
    			    	criteria.andStatusEqualTo("2");
    			    	//criteria.andIsComebackEqualTo(DBConst.FALSE);
    			    }
    			   //过滤付款后已确认收货的，但是退换货的情况
    			    if("2".equals(orderType)){
    			    	criteria.andStatusEqualTo("4");
    			    	//criteria.andIsComebackEqualTo(DBConst.TRUE);
    			    }
    			    
    				Integer count = orderProductsService.count(example);
    			    List<OrderProducts> list = orderProductsService.list(example);
    			    List<Product> products  = new ArrayList<Product>();
    			    if( list.size() > 0 ){
    			    	for( OrderProducts temp : list ){
    			    		Product product = productService.get(temp.getProductId());
    			    		if( null != product ){
    			    			products.add(product);
    			    		}
    			    	}
    			    }
    			    dataMap.put("products", products);
    			    dataMap.put("totalCount", count);
    			    resultMap.put(DATA, dataMap);
    				result = SUCCESS;
        			errorCode = SUCCESS_ERROR_CODE;
                	errorMsg = SUCCESS_ERROR_MSG; 
    			}else{
    				result = LOGIN;
        			errorCode = LOGIN_ERROR_CODE;
                	errorMsg = LOGIN_ERROR_MSG;  	
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	log(e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    } 
    
    
}